// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package androidx.gdata.contacts.xml;

import androidx.gdata.contacts.Contact;
import androidx.gdata.contacts.GroupMembershipInfo;
import androidx.gdata.XMLSerializer;
import androidx.gdata.LinkUrl;
import androidx.gdata.xml.CategorySerializer;
import androidx.gdata.xml.LinkUrlSerializer;

import java.io.PrintWriter;

/**
 * @author jennings
 *         Date: Nov 12, 2008
 */
public class ContactSerializer implements XMLSerializer<Contact> {
  private CategorySerializer categorySerializer;
  private LinkUrlSerializer linkSerializer;
  private GdEmailSerializer emailSerializer;
  private GroupMembershipInfoSerializer gmiSerializer;
  
  public ContactSerializer() {
    categorySerializer = new CategorySerializer();
    linkSerializer = new LinkUrlSerializer();
    emailSerializer = new GdEmailSerializer();
    gmiSerializer = new GroupMembershipInfoSerializer();
    categorySerializer.setExtraIndent("  ");
    linkSerializer.setExtraIndent("  ");
    emailSerializer.setExtraIndent("  ");
    gmiSerializer.setExtraIndent("  ");
  }

  public void serialize(PrintWriter out, Contact contact) {
    out.println("  <entry>");
    out.println("    <id>" + contact.getId() + "</id>");
    out.println("    <updated>" + contact.getUpdated() + "</updated>");
    categorySerializer.serialize(out, contact.getCategory());
    out.println("    <title type='text'>" + contact.getTitle() + "</title>");
    for (LinkUrl link : contact.getLinks()) {
      linkSerializer.serialize(out, link);
    }
    if (contact.getEmail() != null) {
      emailSerializer.serialize(out, contact.getEmail());
    }
    GroupMembershipInfo gmi = contact.getGroupMembership();
    if (gmi != null) {
      gmiSerializer.serialize(out, contact.getGroupMembership());
    }
    out.println("  </entry>");
  }
}
